package GameServer

import (
	"PBMessage"
	. "SGNet"
	proto "github.com/golang/protobuf/proto"
	log "misc/seelog"
	"time"
)

func (this *GameServer) CS_Login_Handler(msg *Message) {
	loginData := &PBMessage.CS_Login{}
	proto.Unmarshal(msg.Data, loginData)
	userName := loginData.GetUserKey()
	player := this.GetPlayer(userName)
	//服务器上角色重名
	if player != nil {
		RetData := &PBMessage.SC_RetLogin{}
		log.Info(loginData.GetUserKey() + " 服务器上有重名")
		RetData.RetType = proto.Int(1)
		RetData.UserID = proto.Int64(0)
		msg.Sess.SendPacket(PBMessage.PBID_SC_RetLogin, RetData)
		return
	}

	newPlayer := NewClientPlayer(msg.Sess)
	newPlayer.UserID = time.Now().UnixNano()
	newPlayer.UserName = loginData.GetUserKey()
	newPlayer.Sess.UserData = newPlayer
	this.AddPlayer(newPlayer)
	//加入大厅
	addRommMessage := &Message{}
	addRommMessage.Type = MESSAGETYPE_CMD
	addRommMessage.Id = CMD_AddHallPlayer
	addRommMessage.Sess = msg.Sess
	addRommMessage.Param = map[string]interface{}{
		"Player": newPlayer,
	}
	GetGameHall().AddMessage(addRommMessage)
}
